<?php

class LobbyView extends View
{
	public function BuildLobby( $lobbySections )
	{
		require_once 'php/obj/LobbySection.php';

		$this->BuildNav( $lobbySections );

		$this->Markup( '        <div id="lobby-container">' . "\r\n" );
		foreach ( $lobbySections as $section )
		{
			switch ( $section->Type() )
			{
				case LobbySection::GAMES_TABLE :
					$this->BuildGamesTable( $section );
					break;
				case LobbySection::CREATE_GAME :
					$this->BuildCreateGame( $section );
					break;
				case LobbySection::GAME_DETAILS :
					$this->BuildGameDetails( $section );
					break;
			}
		}
		
		$this->Markup( '        </div>' . "\r\n" );
	}
	
	private function BuildNav( $lobbySections )
	{
		$this->Markup( '        <ul id="lobby-nav">' . "\r\n" );

		foreach ( $lobbySections as $section )
		{
			$this->Markup( '          <li id="' . $section->ID() . '"' . ( ( strtolower( $section->ID() ) == strtolower( $this->GetTab() ) ) ? ' class="open"' : '' ) . '><a href="#">' . $section->Name() . '</a></li>' . "\r\n" );
		}

		$this->Markup( '        </ul>' . "\r\n" );
	}
	
	private function BuildGameDetails( $section )
	{
		$this->Markup( '          <div id="' . $section->ID() . '-section" class="lobby-section">' . "\r\n" );

		if ( $section->Data()->ID() !== null )
		{
			$this->Markup( '            <dl>' . "\r\n"  );
			$this->Markup( '              <dt>Status</dt>' . "\r\n"  );
			$this->Markup( '              <dd>' . "\r\n" );
			
			if ( $section->Data()->Finished() )
			{
				$this->Markup( 'Finished' . "\r\n"  );
			}
			elseif ( $section->Data()->Started() )
			{
				$this->Markup( '<a href="index.php?c=gameboard&amp;game='.  $section->Data()->ID() .'">Open game</a>' . "\r\n"  );
			}
			elseif ( $section->Data()->MaxPlayers() > count( $section->Data()->Players() ) )
			{
				$this->Markup( 'Waiting for players' . "\r\n"  );	
			}
			else
			{
				if ( Auth::GetUser()->ID() == $section->Data()->Creator()->ID() )
				{
					$this->Markup( 'Ready to start game' . "\r\n" );
				}
				else 
				{
					$this->Markup( 'Waitning for owner to start game' . "\r\n"  );
				}
			}

			$this->Markup( '              </dd>' . "\r\n"  );

			$this->Markup( '              <dt>ID</dt>' . "\r\n"  );
			$this->Markup( '              <dd>' . $section->Data()->ID(). '</dd>' . "\r\n"  );

			$this->Markup( '              <dt>Map</dt>' . "\r\n"  );
			$this->Markup( '              <dd>' . $section->Data()->Map()->Name(). '</dd>' . "\r\n"  );

			$this->Markup( '              <dt>Players (' . count( $section->Data()->Players() ) . '/' . $section->Data()->MaxPlayers() . ')</dt>' . "\r\n"  );
			$this->Markup( '              <dd>' . "\r\n"  );
			$this->Markup( '                <ul>' . "\r\n"  );
			
			foreach ( $section->Data()->Players() as $player )
			{
				if ( $player->User()->ID() == Auth::GetUser()->ID() )
				{
					$playerExists = true;
				}
				
				if ( Auth::GetUser()->ID() == $section->Data()->Creator()->ID() && !$section->Data()->Finished() && !$section->Data()->Started() )
				{
					$this->Markup( '                  <li>' . $player->User()->Name() . ( ( $player->User()->ID() == $section->Data()->Creator()->ID() ) ? ' (OP)' : '' ) . ' <a href="index.php?c=lobby&a=remove&game='.$section->Data()->ID().'&tab=game-details&player=' . $player->User()->ID() .'">' . ( ( Auth::GetUser()->ID() == $player->User()->ID() ) ? 'Delete Game' : 'Remove' ) . '</a></li>' . "\r\n"   );

				}
				elseif ( $player->User()->ID() == Auth::GetUser()->ID() && !$section->Data()->Finished() && !$section->Data()->Started() )
				{
					$this->Markup( '                  <li>' . $player->User()->Name() . ( ( $player->User()->ID() == $section->Data()->Creator()->ID() ) ? ' (OP)' : '' ) . ' <a href="index.php?c=lobby&a=leave&game='.$section->Data()->ID().'&tab=game-details&player=' . $player->User()->ID() .'">Leave</a></li>' . "\r\n"  );
				}
				else
				{
					$this->Markup( '                  <li>' . $player->User()->Name() . ( ( $player->User()->ID() == $section->Data()->Creator()->ID() ) ? ' (OP)' : '' ) .'</li>' . "\r\n"  );
				}
				
			}
			
			$this->Markup( '                </ul>' . "\r\n"  );
			$this->Markup( '              </dd>' . "\r\n"  );
			$this->Markup( '            </dl>' . "\r\n"  );

			if ( 	!$section->Data()->Started() && 
					 $section->Data()->Creator()->ID() == Auth::GetUser()->ID() &&
					 $section->Data()->MaxPlayers() == count( $section->Data()->Players() ) )
			{ 
				$this->Markup( '<ul>' . "\r\n"  );
				$this->Markup( '    <li><a href="index.php?c=lobby&amp;a=start&amp;game='. $section->Data()->ID().'&amp;tab=game-details">Start Game</a></li>' . "\r\n"  );
				$this->Markup( '</ul>' );
			} 

			if ( count( $section->Data()->Players() ) < $section->Data()->MaxPlayers() )
			{
				$this->Markup( '<ul>' . "\r\n"  );
				$this->Markup( '    <li><a href="index.php?c=lobby&a=join&game=' . $section->Data()->ID() . '&tab=game-details&player=' . Auth::GetUser()->ID() .'">Join</a></li>' );
				$this->Markup( '</ul>' . "\r\n"  );
			}
		}
		else
		{
			$this->Markup(            '<p>There are no details for this game..</p>' . "\r\n"  );
		}

		$this->Markup( '          </div>' . "\r\n" );
	}

	private function BuildGamesTable( $section )
	{
		$this->Markup( '          <div id="' . $section->ID() . '-section" class="lobby-section">' . "\r\n" );
		$this->Markup( '            <table>' . "\r\n" );
		$this->Markup( '              <thead>' . "\r\n" );
		$this->Markup( '                <tr>' . "\r\n" );
		$this->Markup( '                  <th>ID</th>' . "\r\n" );
		$this->Markup( '                  <th>Creator</th>' . "\r\n" );
		$this->Markup( '                  <th>Slots</th>' . "\r\n" );
		$this->Markup( '                  <th>Players</th>' . "\r\n" );
		$this->Markup( '                  <th>Map</th>' . "\r\n" );
		$this->Markup( '                  <th>Enter</th>' . "\r\n" );
		$this->Markup( '                </tr>' . "\r\n" );
		$this->Markup( '              </thead>' . "\r\n" );
		$this->Markup( '              <tbody>' . "\r\n" );
		foreach ( $section->Data() as $game )
		{
			$this->Markup( '              <tr');
			if ( $game->Finished() )
			{
				$this->Markup( ' class="finished"' . "\r\n" );
			}
			elseif ( $game->Started() )
			{
				$this->Markup( ' class="started"' . "\r\n" );
			}
			else
			{
				$this->Markup( ' class="open"' . "\r\n" );
			}
			$this->Markup( '>' . "\r\n" );
			$this->Markup( '                <td>' . $game->ID() . '</td>' . "\r\n" );
			$this->Markup( '                <td>' . $game->Creator()->Name() . '</td>' . "\r\n" );
			$this->Markup( '                <td>' . $game->MaxPlayers() . '</td>' . "\r\n" );
			$this->Markup( '                <td>' . count( $game->Players() ) . '</td>' . "\r\n" );
			$this->Markup( '                <td>' . $game->Map()->Name() . '</td>' . "\r\n" );

			if ( Auth::isLoggedIn() )
			{	
				$this->Markup( '                <td><a href="index.php?c=lobby&amp;a=details&amp;game=' . $game->ID() . '&amp;tab=game-details" class="button">Enter</a></td>' . "\r\n" );
			}

			else
			{
				$this->Markup( '                <td><p>Login to see details</p></td>' . "\r\n" );
			}

			$this->Markup( '              </tr>' . "\r\n" );
		}

		$this->Markup( '              </tbody>' . "\r\n" );
		$this->Markup( '            </table>' . "\r\n" );
		$this->Markup( '          </div>' . "\r\n" );
	}

	private function BuildCreateGame( $section )
	{
		$this->Markup( '          <div id="' . $section->ID() . '-section" class="lobby-section">' . "\r\n" );
		$this->Markup( '            <form action="index.php?c=lobby&amp;a=create&amp;tab=my-games" method="POST">' . "\r\n" );
		$this->Markup( '              <fieldset>' . "\r\n" );
		$this->Markup( '                <h3>Create new game</h3>' . "\r\n" );
		
		$this->Markup( '                <label for="maxPlayers">Max Players:</label>' . "\r\n" );
		$this->Markup( '                <select name="maxPlayers" id="maxPlayers">' . "\r\n" );
		for ( $i = 2; $i <= 8; $i++ )
		{
			$this->Markup( '                  <option value="' . $i . '">' . $i . '</option>' . "\r\n" );
		}
		$this->Markup( '                </select>' . "\r\n" );

		$this->Markup( '                <label for="map">Map:</label>' . "\r\n" );
		$this->Markup( '                <select name="map" id="map">' . "\r\n" );
		foreach ( $section->Data() as $map )
		{
			$this->Markup( '                  <option value="' . $map->ID() . '">' . $map->Name() . '</option>' . "\r\n" );
		}
		$this->Markup( '                </select>' . "\r\n" );
		
		$this->Markup( '                <input type="submit" name="create" id="create" value="Create Game" />' . "\r\n" );
		$this->Markup( '              </fieldset>' . "\r\n" );
		$this->Markup( '            </form>' . "\r\n" );
		$this->Markup( '          </div>' . "\r\n" );
	}

	public function GetMaxPlayers()
	{
		return isset( $_POST['maxPlayers'] ) ? $_POST['maxPlayers'] : null;
	}

	public function GetMapID()
	{
		return isset( $_POST['map'] ) ? $_POST['map'] : null;
	}

	public function GetTab()
	{
		return isset( $_GET['tab'] ) ? $_GET['tab'] : null;
	}

	public function GetGameID()
	{
		return isset( $_GET['game'] ) ? $_GET['game'] : null;
	}

	public function GetPlayer()
	{
		return isset( $_GET['player'] ) ? $_GET['player'] : null;
	}
}
